﻿#include "test.h"
// namespace MyBaseTest
void MyBaseTest::print_test(int x) { // 待测函数1
    //PROFILE_SCOPE("Pt1");
    PROFILE_FUNCTION_FULL(); // 记录当前函数的运行时间，自动获取函数名
    for (int i = 1; i <= 2000; i++) {
        std::cout << "Hello, world! 哈哈" << i * x << std::endl;
    }
}

void MyBaseTest::print_test() { // 待测函数2
    //PROFILE_SCOPE("Pt2");
    PROFILE_FUNCTION_FULL();
    //double pi_value = acos(-1.0); // 利用反余弦函数acos特性计算π
    for (int i = 1; i <= 2000; i++) {
        std::cout << "Hello, world!" << sqrt(i) * acos(-1.0) << std::endl;
    }
}

double MyBaseTest::newton_sqrt(double x, double epsilon) {
    // 牛顿迭代法求平方根
    double guess = x / 2.0;
    while (abs(guess * guess - x) > epsilon)
        guess = (guess + x / guess) / 2.0;
    //std::cout << "The square root of " << x << " is " << guess << std::endl;
    return guess;
}

void MyBaseTest::test_newton_sqrt() { // 待测函数3
    //PROFILE_SCOPE("Pt3");
    PROFILE_FUNCTION_FULL();
    for (int i = 1; i <= 2000; i++)
        std::cout << "sqrt(" << i << ") = " << newton_sqrt(i) << ", sqrt(" << i << " * 2) = " << newton_sqrt(i * 2) << std::endl;
}

void MyBaseTest::run_benchmarks() { // 基准测试函数，调用待测函数，可以记录一个总运行时间
    PROFILE_FUNCTION_FULL();
    std::cout << "Running Benchmarks ... \n";
    //std::thread t1(std::bind(print, 2)); // 同名函数还需要转换
    std::thread t1([]() { print_test(2); });
    std::thread t2([]() { print_test(); }); // lambda 表达式调用不同的重载函数进入不同的线程
    //std::thread t3([]() { test_newton_sqrt(); });
    print_test(2);
    print_test();
    //test_newton_sqrt();
    t1.join();
    t2.join();
    //t3.join();
    std::cout << "Benchmarks Complete!\n";
}

